package gougeon.java.solveurdictionnaire;


import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
//import java.util.StringTokenizer;

//import javax.swing.text.html.HTMLDocument.Iterator;

//import android.util.Log;

public class Dictionnaire {

	static HashMap<String, ArrayList<String>> mots = new HashMap<String, ArrayList<String>>();
	
    /**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String fileName = args[0];
		//chaine ma_chaine= new chaine("ABC");
		//System.out.println("I did it ! "+ma_chaine.chaine_id + " "+fileName);
		try {
			LoadFile( fileName, mots);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    //load file from Assets folder  
    public static String LoadFile(String fileName, HashMap<String, ArrayList<String>> storage) throws IOException  
    {  
    	FileInputStream fis = new FileInputStream(fileName); 
    	//InputStreamReader br = new InputStreamReader( fis);
    	Scanner br = new Scanner( fis);
    	String inString;
    	String key="";//,token;
    	//StringTokenizer st = null;
    	
    	//int tokenNumber = 0;
    	try {
    		while (br.hasNextLine())
    		//while( (inString = br.nextLine()) != null)
    
    	{inString = br.nextLine();
    		ArrayList<String> liste_de_mots = new ArrayList<String>();
    		//System.out.println(inString);
          chaine evaluate = new chaine(inString.toUpperCase());
          key=String.valueOf(evaluate.chaine_id);
          liste_de_mots.add(inString);
    		//Log.d("solveur : ","key="+key+" ("+liste_de_mots+")");
    		if (storage.containsKey(key))
    		{
    			// try to group all 
    			storage.get(key).addAll(liste_de_mots);
    			//Log.i("solveur : ","key="+key+" already exist ("+liste_de_mots.toString()+"\" / \""+storage.get(key).toString()+")");
    		}
    		else
    		{
    			storage.put(key, liste_de_mots);
    		}
    	}
    	} finally{
    	      br.close();
        }
    	// dump the data for debug
    /*	if (fileName.equals("8.csv"))*/{ 
    		Iterator<ArrayList<String>> iter = storage.values().iterator();
    		
    		while(iter.hasNext())
    		{
    			int idx_mot;
    			String nice_list="";
    			String curr_mot="";
    			ArrayList<String> currlist=iter.next();
    			for (idx_mot=0; idx_mot<currlist.size(); idx_mot++)
     	        {
    				curr_mot=currlist.get(idx_mot).toLowerCase();
    				nice_list=nice_list+","+curr_mot;
    			}
    			chaine evaluate = new chaine(curr_mot.toUpperCase());
    			//Log.d("solveurQQQQ  "+fileName,""+evaluate.chaine_id+nice_list);
     	        System.out.println(""+evaluate.chaine_id+nice_list);
    		}
    	}/* */
        //return the output stream as a String
        return "dd";  
    }
}
